program define besw14b
use "$input/bes-subset", clear

replace euRefVoteW14 = . if euRefVoteW14 ==  9999

gen leave = 1 if euRefVoteW14 == 1
replace leave = 0 if euRefVoteW14 == 0

replace  anyUniW14  = . if  anyUniW14  == 9999

gen wkstat = 1 if workingStatusW14 == 1 |  workingStatusW14 == 2 | workingStatusW14 ==3 // employed
replace wkstat = 2 if workingStatusW14 == 4 // unemployed
replace wkstat = 3 if workingStatusW14 == 5 | workingStatusW14 == 6  // student
replace wkstat = 4 if workingStatusW14 == 7 // retired 
replace wkstat = 5 if workingStatusW14 == 8 // not in paid work for any other reason

gen married = 0 if profile_marital_statW14 !=.
replace married = 1 if profile_marital_statW14 == 1 | profile_marital_statW14 == 2

gen female = 1 if gender == 2
replace female = 0 if gender == 1

destring supergroup_code , replace
rename supergroup_code onsAreaSupergroupW14
end

program define boe
use "$input/nmg", clear

gen leave = 4 if brexit == "Very positive about the result"  | brexit == "very positive about the result" 
replace leave = 3 if brexit == "Somewhat positive"  | brexit == "somewhat positive"
replace leave = 2 if brexit == "Somewhat negative"  | brexit == "somewhat negative"
replace leave = 1 if brexit == "Very negative"  | brexit == "very negative"

gen leave_bin = 1 if leave == 4 | leave == 3
replace leave_bin = 0 if leave == 2 | leave == 1


gen female = 1 if sex == "female"
replace female = 0 if sex == "male"

rename educ education
gen educ = 0 if education == "GSCE/O-Level/CSE" |  education == "No formal qualification" |  education == " vocational qualification" | education == " Other"   // never attended higher education 
replace educ = 1 if education == "A-Level or equivalent"  // high school
replace educ = 2 if education == "Bachelor Degree or equivalent" | education == "Masters, PhD or equivalent" // higher education

gen wkstat = 1 if workingstatus2014 == "Have pai" | workingstatus2015 =="Self-emp" | workingstatus2015 == "Have paid jo" | workingstatus2015 == "Self-employe" | ///
workingstatus2016 == "Have paid job - full time (30+ hours .." | workingstatus2016 == "Have paid job - part time (up to 29 h.." | workingstatus2016 == "Self-employed" | ///
workingstatus2017 == "Have paid job - full time (30+ hours .." | workingstatus2017 == "Have paid job - part time (up to 29 h.." | workingstatus2017 == "Self-employed" | ///
workingstatus2018 == 1 |  workingstatus2018 == 2 | workingstatus2018 == 3 // employed 
replace wkstat = 2 if workingstatus2014 == "Unemploy" | workingstatus2015 =="Unemployed a" | workingstatus2016 == "Unemployed and seeking work" |  workingstatus2017 == "Unemployed and seeking work" | workingstatus2018 == 5 // unemployed
replace wkstat = 3 if workingstatus2014 ==  "Full tim" | workingstatus2015 =="Full time st" |  workingstatus2016 == "Full time student/still at school" |   workingstatus2017 == "Full time student/still at school" | workingstatus2018 == 4 // student
replace wkstat = 4 if workingstatus2014 == "Retired" | workingstatus2015 == "Retired" |  workingstatus2016 == "Retired" | workingstatus2017 == "Retired" | workingstatus2018 == 6 // retired 
replace wkstat = 5 if workingstatus2014 == "Not in p" | workingstatus2015 == "Not in paid" | workingstatus2016 == "Not in paid work because of long term.." | ///
workingstatus2016 == "Not in paid work for other reason" | workingstatus2017 == "Not in paid work because of long term.." | ///
workingstatus2017 == "Not in paid work for other reason" | workingstatus2018 == 7 | workingstatus2018 == 8 // not in paid work for any other reason


replace fihhyr2_m = "." if fihhyr2_m == "don't know" // household income
replace fihhyr2_m = "." if fihhyr2_m == "refused" // household income
destring fihhyr2_m, replace


encode region, gen(regionid)
end
